//后端表单验证信息转换
export const conversionFormVerification = (messages, errorMsg) => {
  Object.keys(messages).forEach(function(key) {
    (errorMsg[key].status = "error"), (errorMsg[key].message = messages[key]);
  });
  return errorMsg;
};

//初始化后端表单验证信息
export const initFormVerification = errorMsg => {
  Object.keys(errorMsg).forEach(function(key) {
    (errorMsg[key].status = ""), (errorMsg[key].message = "");
  });
  return errorMsg;
};

const getMarryState = val => {
  let res = "";
  switch (val) {
    case 0:
      res = "未婚";
      break;
    case 1:
      res = "已婚";
      break;
    case 2:
      res = "离异";
      break;
    case 3:
      res = "保密";
      break;
  }
  return res;
};

const getEducation = val => {
  let res = "";
  switch (val) {
    case 1:
      res = "小学";
      break;
    case 10:
      res = "初中";
      break;
    case 20:
      res = "高中";
      break;
    case 21:
      res = "中专";
      break;
    case 22:
      res = "技校";
      break;
    case 23:
      res = "中技";
      break;
    case 100:
      res = "大学";
      break;
    case 101:
      res = "大专";
      break;
    case 102:
      res = "专科";
      break;
    case 110:
      res = "本科";
      break;
    case 200:
      res = "研究生";
      break;
    case 201:
      res = "MBA";
      break;
    case 210:
      res = "博士";
      break;
  }
  return res;
};

Date.prototype.Format = function(fmt) {
  var o = {
    "M+": this.getMonth() + 1,
    "d+": this.getDate(),
    "h+": this.getHours(),
    "m+": this.getMinutes(),
    "s+": this.getSeconds(),
    "q+": Math.floor((this.getMonth() + 3) / 3),
    S: this.getMilliseconds()
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
  return fmt;
};

export const toDecimal = x => {
  let ff = parseFloat(x);
  if (isNaN(ff)) {
    return false;
  }
  let f = Math.round(x * 100) / 100;
  let s = f.toString();
  let rs = s.indexOf(".");
  if (rs < 0) {
    rs = s.length;
    s += ".";
  }
  while (s.length <= rs + 2) {
    s += "0";
  }
  return s;
};

// 产品列表
export const productList = [
  // 人像比对
  [
    {
      key: "113",
      code: "background.portrait.compare",
      name: "人像实名比对",
      apiName: "人像实名比对接口",
      isEnable: true,
      url: "/portraitRealName",
      iconIndex: 7,
      introduce: "查询所提供姓名和身份证归属人的身份证存档人像与提供的图片相似程度。"
    },
    {
      key: "114",
      code: "background.portrait.compare.two",
      name: "人像多图比对",
      apiName: "人像多图比对接口",
      isEnable: true,
      url: "/portraitManyPic",
      iconIndex: 6,
      introduce: "查询所提供的两张人像图片的相似程度。"
    }
  ],
  // 信息核验
  [
    {
      key: "103",
      code: "background.mobile.address.check",
      name: "职住比对",
      apiName: "职住比对接口",
      isEnable: true,
      url: "/positionLive",
      iconIndex: 1,
      introduce: "查询所提供手机号归属人的住址信息准确性。"
    },
    {
      key: "102",
      code: "background.idcard.name.check",
      name: "身份证姓名核验",
      apiName: "身份证姓名核验接口",
      isEnable: true,
      url: "/idCardName",
      iconIndex: 2,
      introduce: "「实名核验」查询所提供的身份证与姓名的归属人一致性。"
    },
    {
      key: "100",
      code: "background.mobile.name.check",
      name: "手机姓名核验",
      apiName: "手机姓名核验接口",
      isEnable: true,
      url: "/mobileName",
      iconIndex: 5,
      introduce: "「运营商二要素比对」查询所提供的手机与姓名的归属人一致性。"
    },
    {
      key: "101",
      code: "background.mobile.name.idcard.check",
      name: "三要素核验",
      apiName: "三要素核验接口",
      isEnable: true,
      url: "/idCardMobileName",
      iconIndex: 3,
      introduce: "「运营商核验」查询被查验人提供的姓名、身份证以及手机号是否为同一归属人。"
    },
    {
      key: "106",
      code: "background.bankCard.four.elements",
      name: "银行卡四要素核验",
      apiName: "银行卡四要素核验接口",
      isEnable: true,
      url: "/bankCard",
      iconIndex: 4,
      introduce: "「银行卡鉴权四要素」查询所提供的姓名、身份证、银行卡以及手机号是否为同一归属人。"
    }
  ],
  // 信息查询
  [
    {
      key: "110",
      code: "research.salary.survey",
      name: "薪酬查询",
      apiName: "薪酬查询接口",
      isEnable: true,
      url: "/salary",
      iconIndex: 15,
      introduce: "查询被查验人的薪资收入情况。"
    },
    {
      key: "109",
      code: "research.career.point.query",
      name: "职业分查询",
      apiName: "职业分查询接口",
      isEnable: true,
      url: "/professionGrade",
      iconIndex: 12,
      introduce: "查询被查验人的职场竞争力。"
    },
    {
      key: "105",
      code: "background.mobile.online.status",
      name: "手机在网状态查询",
      apiName: "手机在网状态查询接口",
      isEnable: true,
      url: "/mobileOnlineStatus",
      iconIndex: 10,
      introduce: "「运营商在网状态」查询被查验手机的在网状态。"
    },
    {
      key: "104",
      code: "background.mobile.online.time",
      name: "手机在网时长查询",
      apiName: "手机在网时长查询接口",
      isEnable: true,
      url: "/mobileOnlineTime",
      iconIndex: 9,
      introduce: "「运营商在网时长」查询被查验手机的在网时长。"
    },
    {
      key: "108",
      code: "background.personal.degree",
      name: "学历查询",
      apiName: "学历查询接口",
      isEnable: false,
      url: "/education",
      iconIndex: 11,
      introduce: "查询所提供姓名和身份证归属人的学历记录。"
    },
    {
      key: "112",
      code: "background.personal.degree.score",
      name: "学历分查询",
      apiName: "学历分查询接口",
      isEnable: false,
      url: "/educationScore",
      iconIndex: 13,
      introduce: "查询所提供姓名和身份证归属人的学历转换分值。"
    },
    {
      key: "115",
      code: "background.person.recognition",
      name: "自然人识别",
      apiName: "自然人识别接口",
      isEnable: true,
      url: "/naturePeople",
      iconIndex: 8,
      introduce: "「不良核实」查询所提供姓名和身份证归属人的公安不良记录。"
    },
    {
      key: "111",
      code: "research.crowd.portrait.query",
      name: "人群画像",
      apiName: "人群画像接口",
      isEnable: true,
      url: "/crowdPortrait",
      iconIndex: 14,
      introduce: "查询被查验人的履历信息。"
    },
    {
      key: "116",
      code: "background.sesame.score",
      name: "芝麻信用分",
      apiName: "芝麻信用分查询",
      isEnable: true,
      url: "/sesameCredit",
      iconIndex: 16,
      introduce: "查询被查验人的芝麻信用分。"
    },
    {
      key: "117",
      code: "background.loan.credit",
      name: "四大借条逾期查询",
      apiName: "四大借条逾期查询",
      isEnable: true,
      url: "/fourIOU",
      iconIndex: "IOU",
      introduce: "黑名单比对被查询人是否命中网贷黑名单以及在四大借条的逾期情况。"
    }
  ]
];

const apiListing = () => {
  var apilist = [
    {
      key: "0",
      code: "summary",
      name: "简介",
      apiName: "简介",
      isEnable: true
    },
    {
      key: "113",
      code: "background.portrait.compare",
      name: "人像实名比对",
      apiName: "人像实名比对接口",
      isEnable: true,
      url: "/portraitRealName",
      iconIndex: 7,
      introduce: "查询所提供姓名和身份证归属人的身份证存档人像与提供的图片相似程度。"
    },
    {
      key: "114",
      code: "background.portrait.compare.two",
      name: "人像多图比对",
      apiName: "人像多图比对接口",
      isEnable: true,
      url: "/portraitManyPic",
      iconIndex: 6,
      introduce: "查询所提供的两张人像图片的相似程度。"
    },
    {
      key: "110",
      code: "research.salary.survey",
      name: "薪酬查询",
      apiName: "薪酬查询接口",
      isEnable: true,
      url: "/salary",
      iconIndex: 15,
      introduce: "查询被查验人的薪资收入情况。"
    },
    {
      key: "109",
      code: "research.career.point.query",
      name: "职业分查询",
      apiName: "职业分查询接口",
      isEnable: true,
      url: "/professionGrade",
      iconIndex: 12,
      introduce: "查询被查验人的职场竞争力。"
    },
    {
      key: "111",
      code: "research.crowd.portrait.query",
      name: "人群画像",
      apiName: "人群画像接口",
      isEnable: true,
      url: "/crowdPortrait",
      iconIndex: 14,
      introduce: "查询被查验人的履历信息。"
    },
    {
      key: "103",
      code: "background.mobile.address.check",
      name: "职住比对",
      apiName: "职住比对接口",
      isEnable: true,
      url: "/positionLive",
      iconIndex: 1,
      introduce: "查询所提供手机号归属人的住址信息准确性。"
    },
    {
      key: "102",
      code: "background.idcard.name.check",
      name: "身份证姓名核验",
      apiName: "身份证姓名核验接口",
      isEnable: true,
      url: "/idCardName",
      iconIndex: 2,
      introduce: "「实名核验」查询所提供的身份证与姓名的归属人一致性。"
    },
    {
      key: "100",
      code: "background.mobile.name.check",
      name: "手机姓名核验",
      apiName: "手机姓名核验接口",
      isEnable: true,
      url: "/mobileName",
      iconIndex: 5,
      introduce: "「运营商二要素比对」查询所提供的手机与姓名的归属人一致性。"
    },
    {
      key: "101",
      code: "background.mobile.name.idcard.check",
      name: "三要素核验",
      apiName: "三要素核验接口",
      isEnable: true,
      url: "/idCardMobileName",
      iconIndex: 3,
      introduce: "「运营商核验」查询被查验人提供的姓名、身份证以及手机号是否为同一归属人。"
    },
    {
      key: "106",
      code: "background.bankCard.four.elements",
      name: "银行卡四要素核验",
      apiName: "银行卡四要素核验接口",
      isEnable: true,
      url: "/bankCard",
      iconIndex: 4,
      introduce: "「银行卡鉴权四要素」查询所提供的姓名、身份证、银行卡以及手机号是否为同一归属人。"
    },
    {
      key: "105",
      code: "background.mobile.online.status",
      name: "手机在网状态查询",
      apiName: "手机在网状态查询接口",
      isEnable: true,
      url: "/mobileOnlineStatus",
      iconIndex: 10,
      introduce: "「运营商在网状态」查询被查验手机的在网状态。"
    },
    {
      key: "104",
      code: "background.mobile.online.time",
      name: "手机在网时长查询",
      apiName: "手机在网时长查询接口",
      isEnable: true,
      url: "/mobileOnlineTime",
      iconIndex: 9,
      introduce: "「运营商在网时长」查询被查验手机的在网时长。"
    },
    {
      key: "108",
      code: "background.personal.degree",
      name: "学历查询",
      apiName: "学历查询接口",
      isEnable: false,
      url: "/education",
      iconIndex: 11,
      introduce: "查询所提供姓名和身份证归属人的学历记录。"
    },
    {
      key: "112",
      code: "background.personal.degree.score",
      name: "学历分查询",
      apiName: "学历分查询接口",
      isEnable: false,
      url: "/educationScore",
      iconIndex: 13,
      introduce: "查询所提供姓名和身份证归属人的学历转换分值。"
    },
    {
      key: "115",
      code: "background.person.recognition",
      name: "自然人识别",
      apiName: "自然人识别接口",
      isEnable: true,
      url: "/naturePeople",
      iconIndex: 8,
      introduce: "「不良核实」查询所提供姓名和身份证归属人的公安不良记录。"
    },
    {
      key: "116",
      code: "background.sesame.score",
      name: "芝麻信用分",
      apiName: "芝麻信用分查询",
      isEnable: true,
      url: "/sesameCredit",
      iconIndex: 16,
      introduce: "查询被查验人的芝麻信用分。"
    },
    {
      key: "117",
      code: "background.loan.credit",
      name: "四大借条逾期查询",
      apiName: "四大借条逾期查询",
      isEnable: true,
      url: "/fourIOU",
      iconIndex: "IOU",
      introduce: "黑名单比对被查询人是否命中网贷黑名单以及在四大借条（借贷宝、无忧贷、今借到、米兜凭证）的逾期情况。"
    }
  ];
  // for (var i in productList) {
  //   for (var j in productList[i]) {
  //     apilist.push(productList[i][j]);
  //   }
  // }

  return apilist;
};

// API列表
export const apiList = () => {
  return apiListing();
};

const apiCodeObj = () => {
  var apiList = apiListing();
  var apiObj = {};
  for (var i = 0; i < apiList.length; i++) {
    apiObj[apiList[i].key] = apiList[i];
  }

  return apiObj;
};

export const getApiNameByCode = code => {
  var apiObj = apiCodeObj();
  return apiObj[code].name;
};

export const getApiCodeBykey = key => {
  var apiObj = apiCodeObj();
  return apiObj[key].code;
};

export const date = () => {
  let date = new Date();
  // 年
  let year = date.getFullYear();
  // 月
  let month = date.getMonth() + 1;
  if (month < 10) month = "0" + String(month);
  // 日
  let day = date.getDate();
  if (day < 10) day = "0" + String(day);
  // 时
  let h = date.getHours();
  if (h < 10) h = "0" + String(h);
  // 分
  let min = date.getMinutes();
  if (min < 10) min = "0" + String(min);
  // 秒
  let s = date.getSeconds();
  if (s < 10) s = "0" + String(s);

  return year + "-" + month + "-" + day + " " + h + ":" + min + ":" + s;
};

export const getMonth = () => {
  let date = new Date();
  // 年
  let year = date.getFullYear();
  // 月
  let month = date.getMonth() + 1;
  if (month < 10) month = "0" + String(month);

  return year + "-" + month;
};

export const getProfessionGradeData = (data, map) => {
  let result = [];

  result.push({ queryCondition: "学历", content: data.educationDegree, valid: map.degreeAccuracy });
  result.push({ queryCondition: "学校等级", content: data.schoolGrade, valid: map.schoolAccuracy });
  result.push({ queryCondition: "就业城市规模", content: data.citySize, valid: map.citySizeAccuracy });
  result.push({ queryCondition: "城市人均收入", content: data.cityIncome, valid: map.cityIncomeAccuracy });
  result.push({ queryCondition: "持续工作年限", content: data.workAge, valid: map.workAgeAccuracy });
  result.push({ queryCondition: "收入水平", content: data.personIncome, valid: map.personIncomeAccuracy });
  result.push({ queryCondition: "收入增长性", content: data.incomeIncr, valid: map.incomeIncrAccuracy });
  if (data.companyGrade) {
    result.push({ queryCondition: "公司等级", content: data.companyGrade, valid: map.companyGradeAccuracy });
  }
  if (data.positionGrade) {
    result.push({ queryCondition: "岗位等级", content: data.positionGrade, valid: map.positionGradeAccuracy });
  }
  return result;
};

// export const objToStr = data => {
//   var arr = [];
//   var noArr = ["page", "method", "currentPage", "maxPage", "nextPage", "pageCount", "pageNo", "pageSize", "prevPage", "totalResultCount"];
//   for (var i in data) {
//     if (data[i] && data[i] != null && data[i] != undefined && noArr.indexOf(i) == -1) {
//       var str = i + "=" + data[i];
//       arr.push(str);
//     }
//   }
//   return arr.join("&");
// };

// 接口测试请求 url
export const systemConfig = {
  distApiTestUrl: "https://api.youbenshuju.com/router/rest.htm",
  testApiTestUrl: "http://ybapi.lie100.net/router/rest.htm"
};

// SDK 下载
export const sdkDownload = [
  {
    langua: "JAVA",
    href: "http://statics.lie100.com/downloads/sdk/ybdata-apiclient-java-sdk-1.0.zip"
  },
  {
    langua: "PHP",
    href: "http://statics.lie100.com/downloads/sdk/ybdata-apiclient-php-sdk-1.0.zip"
  },
  {
    langua: "PYTHON",
    href: "http://statics.lie100.com/downloads/sdk/ybdata-apiclient-python-sdk-1.0.zip"
  }
];

// 批量查询 xlsx 模板
export const batchXlsxTem = {
  "100": "http://statics.lie100.com/downloads/templates/contact_verification_template.xlsx", // 手机姓名核验
  "101": "http://statics.lie100.com/downloads/templates/contact_ID_verification_template.xlsx", // 三要素核验
  "102": "http://statics.lie100.com/downloads/templates/ID_verification_template.xlsx", // 身份证姓名核验
  "103": "http://statics.lie100.com/downloads/templates/address_verification_template.xls", // 职住比对
  "104": "http://statics.lie100.com/downloads/templates/phone_Duration_verification_template.xlsx", // 手机在网时长查询
  "105": "http://statics.lie100.com/downloads/templates/phone_Status_verification_template.xlsx", // 手机在网状态查询
  "106": "http://statics.lie100.com/downloads/templates/bankCard_verification_template.xlsx", // 银行卡四要素核验
  "109": "http://statics.lie100.com/downloads/templates/job_evaluation_template.xlsx", // 职业分查询
  "110": "http://statics.lie100.com/downloads/templates/salary_evaluation_template.xlsx", // 薪酬查询
  "111": "http://statics.lie100.com/downloads/templates/demographic_query_template.xlsx", // 人群画像
  "115": "http://statics.lie100.com/downloads/templates/identification_of_natural_person.xlsx" // 自然人识别
};
